In this example in the File SceneDelegate.swif we create user Instance and put it inside the Environment.
We also define User Class in a separate File User.swift.
User.swift (Create)
import SwiftUI
class User : ObservableObject {
@Published var userName : String
@Published var password : String
@Published var emailAddress : String
init(userName: String, password: String, emailAddress: String) {
self.userName = userName
self.password = password
self.emailAddress = emailAddress
}
}
SceneDelegate.swift (Update)
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var user = User(userName: "John", password: "mypassword", emailAddress: "john@gmail.com")
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions:
UIScene.ConnectionOptions) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView().environmentObject(user)
Store multiple Instances
let contentView = ContentView()
.environmentObject(firstBindable)
.environmentObject(secondBindable)